package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.exceptions.OpcBizException;
import com.csun.cmny.provider.mapper.OpcMessagePushWayMapper;
import com.csun.cmny.provider.model.domain.OpcFlowMessageWay;
import com.csun.cmny.provider.model.domain.OpcMessagePushWay;
import com.csun.cmny.provider.model.dto.message.MessagePushWayQueryDto;
import com.csun.cmny.provider.service.OpcFlowMessageGroupWayService;
import com.csun.cmny.provider.service.OpcFlowMessageWayService;
import com.csun.cmny.provider.service.OpcMessagePushWayService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 11:07
 * @Description:
 */
@Service
@Transactional
public class OpcMessagePushWayServiceImpl extends BaseService<OpcMessagePushWay> implements OpcMessagePushWayService {

    @Resource
    private OpcMessagePushWayMapper opcMessagePushWayMapper;
    @Resource
    private OpcFlowMessageWayService opcFlowMessageWayService;

    /**
     * add opc message push way
     *
     * @param opcMessagePushWay the opc message push way
     * @param loginAuthDto      the login auth dto
     * @return the int.
     */
    @Override
    public Integer add(OpcMessagePushWay opcMessagePushWay, LoginAuthDto loginAuthDto) {

        OpcMessagePushWay opcMessagePushWayQuery = new OpcMessagePushWay();
        opcMessagePushWayQuery.setCode(opcMessagePushWay.getCode());
        opcMessagePushWayQuery = opcMessagePushWayMapper.selectOne(opcMessagePushWayQuery);
        if (PublicUtil.isNotEmpty(opcMessagePushWayQuery)) {
            throw new OpcBizException(ErrorCodeEnum.OPC10041001);
        }

        opcMessagePushWay.setUpdateInfo(loginAuthDto);

        return opcMessagePushWayMapper.insertSelective(opcMessagePushWay);
    }

    /**
     * edit opc message push way
     *
     * @param opcMessagePushWay the opc message push way
     * @param loginAuthDto      the login auth dto
     * @return the int.
     */
    @Override
    public Integer edit(OpcMessagePushWay opcMessagePushWay, LoginAuthDto loginAuthDto) {

        Long id = opcMessagePushWay.getId();

        // 查询Id是否存在
        OpcMessagePushWay opcMessagePushWayQuery = opcMessagePushWayMapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(opcMessagePushWayQuery)) {
            throw new OpcBizException(ErrorCodeEnum.OPC10041002);
        }

        String code = opcMessagePushWay.getCode();
        if (PublicUtil.isNotEmpty(code)) {
            opcMessagePushWayQuery = new OpcMessagePushWay();
            opcMessagePushWayQuery.setCode(code);
            opcMessagePushWayQuery = opcMessagePushWayMapper.selectOne(opcMessagePushWayQuery);
            if (PublicUtil.isNotEmpty(opcMessagePushWayQuery)
                    && !opcMessagePushWayQuery.getId().equals(id)) {
                throw new OpcBizException(ErrorCodeEnum.OPC10041001);
            }
        }

        return opcMessagePushWayMapper.updateByPrimaryKeySelective(opcMessagePushWay);
    }

    /**
     * delete opc message push way
     *
     * @param id the id
     * @return the int.
     */
    @Override
    public Integer deleteById(Long id) {

        // 判断是否在使用
        OpcFlowMessageWay opcFlowMessageWay = new OpcFlowMessageWay();
        opcFlowMessageWay.setWayId(id);
        int count = opcFlowMessageWayService.selectCount(opcFlowMessageWay);
        if (count > 0) {
            throw new OpcBizException(ErrorCodeEnum.OPC10041003);
        }

        return opcMessagePushWayMapper.deleteByPrimaryKey(id);
    }

    /**
     * query opc message push way
     *
     * @param messagePushWayQueryDto the message push way query dto
     *                               
     * @return the list.
     */
    @Override
    public List<OpcMessagePushWay> queryListWithPage(MessagePushWayQueryDto messagePushWayQueryDto) {

        return opcMessagePushWayMapper.queryListWithPage(messagePushWayQueryDto);
    }
}
